package mysql

import (
	"cloud-storage/global"
	"errors"
	"gorm.io/gorm"
)

type JwtBlackList struct {
	Id    int    `gorm:"autoIncrement;primaryKey;comment:'编号'"`
	Token string `gorm:"comment:'拉黑的token'"`
}

// JsonInBlackList 在数据库中创建一个表用于存放黑名单token
func JsonInBlackList(jwtList JwtBlackList) (err error) {
	err = global.DB.AutoMigrate(&JwtBlackList{})
	err = global.DB.Create(&jwtList).Error
	return
}

// IsBlackList 判断jwt是否在黑名单
func IsBlackList(jwt string) bool {
	err := global.DB.Where("token = ?", jwt).First(JwtBlackList{}).Error
	isNotFound := errors.Is(err, gorm.ErrRecordNotFound)
	return !isNotFound
}
